package view.component.weapon.flute
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Screen;
	import feathers.controls.ScreenNavigator;
	import feathers.controls.ScreenNavigatorItem;
	import feathers.controls.ScrollContainer;
	import feathers.controls.TabBar;
	import feathers.data.ListCollection;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	import util.WeaponUtil;
	
	import view.component.weapon.flute.staff.StaffType014View;
	import view.component.weapon.flute.staff.StaffType031View;
	import view.component.weapon.flute.staff.StaffType034View;
	import view.component.weapon.flute.staff.StaffType035View;
	import view.component.weapon.flute.staff.StaffType045View;
	import view.component.weapon.flute.staff.StaffType051View;
	import view.component.weapon.flute.staff.StaffType061View;
	import view.component.weapon.flute.staff.StaffType063View;
	import view.component.weapon.flute.staff.StaffType064View;
	import view.component.weapon.flute.staff.StaffType065View;
	import view.component.weapon.flute.staff.StaffType714View;
	import view.component.weapon.flute.staff.StaffType721View;
	import view.component.weapon.flute.staff.StaffType726View;
	import view.component.weapon.flute.staff.StaffType731View;
	import view.component.weapon.flute.staff.StaffType732View;
	import view.component.weapon.flute.staff.StaffType734View;
	import view.component.weapon.flute.staff.StaffType735View;
	import view.component.weapon.flute.staff.StaffType742View;
	import view.component.weapon.flute.staff.StaffType745View;
	import view.component.weapon.flute.staff.StaffType751View;
	import view.component.weapon.flute.staff.StaffType752View;
	import view.component.weapon.flute.staff.StaffType761View;
	import view.component.weapon.flute.staff.StaffType763View;
	import view.component.weapon.flute.staff.StaffType764View;
	import view.component.weapon.flute.staff.StaffType765View;
	import view.component.weapon.flute.staff.StaffTypeCommonView;
	import view.transition.AnimationNaviTransitionManager;
	
	
	/**
	 * 狩猎笛-乐谱信息
	 * @author £iang
	 * 
	 */	
	public class FluteStaffView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 音色选项 */
		public var melodyTabBar:TabBar;
		/** 音色选项滚动条容器 */
		public var melodyTabSC:ScrollContainer;
		/** 乐谱信息音色导航栏 */
		public var melodyNavigator:ScreenNavigator;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 转场管理 */
		private var _transitManager:AnimationNaviTransitionManager;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			var labelVec:Vector.<String>;
			var dataProvider:ListCollection = new ListCollection( [] );
			var len:int;
			var i:int;
			var obj:Object;
			
			labelVec = WeaponUtil.getMelodyTypeTextVec();
			labelVec.splice( 0, 0, "共通" );
			
			melodyTabBar = new TabBar();
			melodyTabBar.gap = 15 * this.dpiScale;
			len = labelVec.length;
			
			for ( i = 0; i < len; i ++ )
			{
				obj = new Object();
				obj.label = labelVec[ i ];
				dataProvider.push( obj );
			}
			
			melodyTabBar.dataProvider = dataProvider;
			melodyTabBar.tabFactory = mainTabFactory;
			melodyTabSC = new ScrollContainer();
			this.addChild( melodyTabSC );
			melodyTabSC.addChild( melodyTabBar );
			
			this.melodyNavigator = new ScreenNavigator();
			this.addChild( melodyNavigator );
			
			this.melodyNavigator.addScreen(
				"INDEX0",
				new ScreenNavigatorItem( StaffTypeCommonView )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX1",
				new ScreenNavigatorItem( StaffType061View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX2",
				new ScreenNavigatorItem( StaffType014View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX3",
				new ScreenNavigatorItem( StaffType051View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX4",
				new ScreenNavigatorItem( StaffType031View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX5",
				new ScreenNavigatorItem( StaffType064View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX6",
				new ScreenNavigatorItem( StaffType065View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX7",
				new ScreenNavigatorItem( StaffType063View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX8",
				new ScreenNavigatorItem( StaffType045View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX9",
				new ScreenNavigatorItem( StaffType034View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX10",
				new ScreenNavigatorItem( StaffType035View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX11",
				new ScreenNavigatorItem( StaffType721View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX12",
				new ScreenNavigatorItem( StaffType726View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX13",
				new ScreenNavigatorItem( StaffType752View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX14",
				new ScreenNavigatorItem( StaffType761View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX15",
				new ScreenNavigatorItem( StaffType714View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX16",
				new ScreenNavigatorItem( StaffType751View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX17",
				new ScreenNavigatorItem( StaffType731View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX18",
				new ScreenNavigatorItem( StaffType764View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX19",
				new ScreenNavigatorItem( StaffType765View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX20",
				new ScreenNavigatorItem( StaffType763View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX21",
				new ScreenNavigatorItem( StaffType745View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX22",
				new ScreenNavigatorItem( StaffType734View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX23",
				new ScreenNavigatorItem( StaffType735View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX24",
				new ScreenNavigatorItem( StaffType742View )
			);
			
			this.melodyNavigator.addScreen(
				"INDEX25",
				new ScreenNavigatorItem( StaffType732View )
			);
			
			this._transitManager = new AnimationNaviTransitionManager( melodyNavigator );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{
			melodyTabSC.y = UISize.MAIN_H - UISize.MONSTER_TAB_AREA_H * this.dpiScale * 3 
				+ 40 * this.dpiScale;
			melodyTabSC.x = 20 * this.dpiScale;
			melodyTabSC.width = UISize.MAIN_W - 40 * this.dpiScale;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		private function addListener():void
		{
			melodyTabBar.addEventListener( Event.CHANGE, tabBarChangeHandler );
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		public function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W3, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		/**
		 * 清除导航栏 
		 * 
		 */		
		public function clearNavi():void
		{
			_transitManager.clear();
			melodyNavigator.removeAllScreens();
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */		
		public function playHideAnimation():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0);
			Starling.juggler.add( tween );
		}
		
		/**
		 * 阅读某一种音色的旋律 
		 * @param melodyType 音色种类
		 * 
		 */		
		public function gotoMelody( melodyType:int ):void
		{
			var scW:Number = melodyTabSC.width;
			var maxW:Number = ( UISize.TAB_BUTTON_W3 * 26 + 15 * 25 )  * this.dpiScale;
			var tmpW:Number = ( melodyType + 1 ) * ( UISize.TAB_BUTTON_W3 + 15 ) * this.dpiScale;
			var hPos:Number;
			
			if ( tmpW > maxW - scW )
				hPos = melodyTabSC.maxHorizontalScrollPosition;
			else
				hPos = tmpW;
			
			melodyTabBar.selectedIndex = melodyType + 1;
			melodyTabSC.horizontalScrollPosition = hPos;
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理tabBar改变时派发的事件 
		 * @param event tabBar改变时派发的事件 
		 * 
		 */		
		protected function tabBarChangeHandler( event:Event ):void
		{
			if ( melodyTabBar.selectedIndex >= 0 )
				this.melodyNavigator.showScreen( "INDEX" + melodyTabBar.selectedIndex );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( this );
			
			var i:int;
			var button:Button;
			
			for ( i = 0; i < melodyTabBar.numChildren; i ++ )
			{
				if ( melodyTabBar.getChildAt( i ) is Button )
				{
					button = melodyTabBar.getChildAt( i ) as Button;
					button.dispose();
					FG2FUGenerator.deepDisposeButtonComp( button );
					button = null;
				}
			}
			
			melodyTabSC.dispose(); melodyTabSC = null;
			
			melodyNavigator.dispose();
			melodyNavigator = null;
			_transitManager = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function FluteStaffView()
		{
			super();
		}
	}
}